<?php
namespace app\index\controller;

use app\common\model\Exchange;
use app\common\model\PieceShare;
use app\common\model\User as UserModel;
use app\lib\exception\BaseException;
use think\Db;

class User extends BaseController
{
    public function getByOpenId($openid)
    {
        return (new UserModel())->getByOpenId($openid);
    }

    public function getExchanges()
    {
        $exchanges = Exchange::where(['user_id' => $this->user_info['id']])->
        with('product')->order('create_time DESC')->select();
        $exchanges = collection($exchanges)->toArray();
        $this->assign('exchanges', $exchanges);
        return $this->fetch();
    }

    public function productCode()
    {
        $exchange_id = input('exchange_id');
        //生成加密字符串
        $salt = config('common.code_salt');
        $url = config('common.domain') . url('index/Admin/exchange');
        $sign = md5($exchange_id . $salt);
        $url .= '?sign=' . $sign . '&exchange_id=' . $exchange_id;
        return successReturn(['url' => $url], '请出示给店员！');
    }
    public function record(){
        $res = PieceShare::getByUserId($this->user_info['id'],
            ['id','is_received','receiver_id','receiver_nickname','receive_time','piece_id','product_id']);
        $res = collection($res)->toArray();
        $this->assign('records',$res);
        //页面分享设置信息
        $share_info = model('page')->where('name="piece"')->find();
        if(!$share_info) throw new BaseException(['无页面分享信息！']);
        $share_info = $share_info->toArray();
        $this->assign('share_info',$share_info);
        return successReturn($this->fetch(),null);
    }


}
